#include <iostream>
#include "include/mysql.h"
#include <cstdio>
#include <string>

using namespace std;
const std::string host = "127.0.0.1";
const std::string user = "root";
const std::string passwd = "fengying66666..";
const std::string db = "tmp";
const unsigned int port = 8080;

int main()
{
    cout << "mysql client version : " << mysql_get_client_info() << endl; 
    //1.创建一个MySQL句柄
    MYSQL* my = mysql_init(nullptr);
    //设置编码格式
    mysql_set_character_set(my, "utf8");
    //2.连接数据库
    if(mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0) == nullptr)
    {
        cout << mysql_error(my) << endl;
        cout << "connect falied" << endl;
        return 1;
    }
    //3.执行sql语句
    // const std::string sql = "insert into t2 values(7, '迪迦', '男')";
    // int ret = mysql_query(my, sql.c_str());
    // cout << "ret = " << ret << endl;

    //编写查询语句
    const std::string sql = "select * from t2";
    int ret = mysql_query(my, sql.c_str());
    MYSQL_RES* code = mysql_store_result(my);
    my_ulonglong row = mysql_num_rows(code);
    unsigned int col = mysql_num_fields(code);
    MYSQL_FIELD* field = mysql_fetch_fields(code);
    for(int i = 0;i < col;++i)
    {
        cout << field[i].name << "\t";
    }
    cout << endl;
    for(int i = 0;i < row;++i)
    {
        MYSQL_ROW line = mysql_fetch_row(code);
        for(int j = 0;j < col;++j)
        {
            cout << line[j] << "\t";
        }
        cout << endl;
    }

    //4.关闭数据库
    mysql_close(my);

    return 0;
}